home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_11_11
/
labrocca
/
dyn2darr.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-05-20
|
1KB
|
45 lines
/* DYN2DARR.C */
/* Copyright 1993 by P. J. LaBrocca
All rights reserved.
Compiles with Microsoft C versions 6, 7 & 8 (MS-DOS)
and Symantec THINK C 5 (Macintosh).
*/
#include <stdlib.h>
/******************
Allocates a block of memory that can
be accessed as a two-dimensional
array using subscript operators.
Use macro defined in DYN2DARR.H, Dyn2dArray,
instead of function.
*******************/
void **dyn2darray( unsigned row, unsigned col,
unsigned el_size )
{
unsigned c;
char *p = NULL;
void **arr = (void **)
calloc( row * sizeof( void * ) + /* for pointers */
row * col * el_size + /* for elements */
2 * sizeof( unsigned ), /* repetition counts */
sizeof( char ) );
if( arr == NULL )
return NULL;
/* Store row and col */
p = (char *) arr + row * sizeof( void * );
* (unsigned *) p = row;
p += sizeof( unsigned ); /* p is a pointer to char */
* (unsigned *) p = col;
p += sizeof( unsigned );
/* Link rows to pointers */
for( c = 0; c < row; ++c ) {
arr[c] = p + ( c * col * el_size) ;
}
return arr;
}
/* End of File */